package cn.lf.android.lawfirm.worklog.controller;


import android.content.Context;
import android.util.Pair;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.lf.android.lawfirm.AppConfig;
import cn.lf.android.lawfirm.BaseController;
import cn.lf.android.lawfirm.http.manager.EBCallback;
import cn.lf.android.lawfirm.http.manager.MyOkHttpClient;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.utils.SPUtil;

import static android.R.attr.path;

/**
 * 添加
 */
public class AddWorkLogConroller extends BaseController {
    AddWorkLogCallback mAddWorkLogCallback;
    private Context context;

    public AddWorkLogConroller(AddWorkLogCallback callback, Context context) {
        this.mAddWorkLogCallback = callback;
        this.context = context;
    }


    /**
     * @param linkmanID
     * @param checkuserid
     * @param LsysLogID
     * @param logtype
     * @param LogTheme
     * @param LogWorkDate
     * @param LogWorkTime
     * @param LogWorkTimeMin
     * @param LogLogOnOrder
     * @param LogOnOrderMin
     * @param LogCaseID
     * @param LogWorkType
     * @param LogWorkContentText
     * @param casefilesid
     * @param LogNodeID
     */
    public void onAddWorkLog(String linkmanID, String checkuserid, String LsysLogID, String logtype, String LogTheme, String LogWorkDate, String LogWorkTime, String LogWorkTimeMin,
                             String LogLogOnOrder, String LogOnOrderMin, String LogCaseID,
                             String LogWorkType, String LogWorkContentText, String casefilesid, String LogNodeID, List<String> path) {
        showProgressDia(context);
        Map<String, String> params = new HashMap<String, String>();
        SPUtil spUtil = new SPUtil(context);
        params.put("linkmanID", linkmanID);
        params.put("checkuserid", checkuserid);
        params.put("LsysLogID", LsysLogID);
        params.put("logtype", logtype);
        params.put("LogTheme", LogTheme);
        params.put("LogWorkDate", LogWorkDate);
        params.put("LogWorkTime", LogWorkTime);
        params.put("LogWorkTimeMin", LogWorkTimeMin);
        params.put("LogLogOnOrder", LogLogOnOrder);
        params.put("LogOnOrderMin", LogOnOrderMin);
        params.put("LogCaseID", LogCaseID);
        params.put("LogWorkType", LogWorkType);
        params.put("LogWorkContentText", LogWorkContentText);
        params.put("casefilesid", casefilesid);
        params.put("LogNodeID", LogNodeID);
        DebugLog.e("checkuserid===" + checkuserid);
//        List<Pair<String, File>> list = new ArrayList<Pair<String, File>>();
//        Pair<String, File> pair = new Pair<String, File>("imgs", new File(
//                path));
//        list.add(pair);

        List<Pair<String, File>> list = new ArrayList<Pair<String, File>>();
        for (int i = 0; i < path.size(); i++) {
            Pair<String, File> pair = new Pair<String, File>("imgs",
                    new File(path.get(i)));
            list.add(pair);
        }

        DebugLog.e("linkmanID===" + linkmanID);
        DebugLog.e("checkuserid===" + checkuserid);
        DebugLog.e("LsysLogID===" + LsysLogID);
        DebugLog.e("logtype===" + logtype);
        DebugLog.e("LogTheme===" + LogTheme);
        DebugLog.e("LogWorkDate===" + LogWorkDate);
        DebugLog.e("LogWorkTime===" + LogWorkTime);
        DebugLog.e("LogWorkTimeMin===" + LogWorkTimeMin);
        DebugLog.e("LogLogOnOrder===" + LogLogOnOrder);
        DebugLog.e("LogOnOrderMin===" + LogOnOrderMin);
        DebugLog.e("LogCaseID===" + LogCaseID);
        DebugLog.e("LogWorkType===" + LogWorkType);
        DebugLog.e("LogWorkContentText===" + LogWorkContentText);
        DebugLog.e("casefilesid===" + casefilesid);
        DebugLog.e("LogNodeID===" + LogNodeID);

        new MyOkHttpClient().uploadFiles(AppConfig.LsysLog_SaveLogInfo, params, list,
                new EBCallback() {
                    @Override
                    public void onEBResponse(String response) {
                        DebugLog.e("添加编辑工作日誌===" + response);
                        JSONObject myJsonObject;
                        Boolean isSuc;
                        try {
                            myJsonObject = new JSONObject(response);
                            isSuc = myJsonObject.getBoolean("isSuc");
                            if (isSuc) {
                                mAddWorkLogCallback.onAddWorkLogSuccessed("");
                            } else {
                                mAddWorkLogCallback
                                        .onAddWorkLogFailed();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        dismissProgressDia();
                    }

                    @Override
                    public void onEBError(String errorMsg) {
                        DebugLog.e("添加编辑工作日誌失败===" + errorMsg);
                        mAddWorkLogCallback.onAddWorkLogFailed();
                        dismissProgressDia();
                    }
                }, "Authorization", spUtil.getAccessToken());
    }


    public interface AddWorkLogCallback {
        public void onAddWorkLogSuccessed(String str);

        public void onAddWorkLogFailed();

    }

}
